package com.gugee.tiktok.web.controller;

import com.gugee.common.web.util.CheckMobile;
import com.gugee.tiktok.web.dto.HelpCenterArticleSummaryDto;
import com.gugee.tiktok.web.dto.HelpCenterMenuDto;
import com.gugee.tiktok.web.model.mysql.HelpArticle;
import com.gugee.tiktok.web.service.HelperService;
import com.xiguaji.boot.toolkit.JsonUtil;
import com.xiguaji.boot.toolkit.jdbc.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@Slf4j
//@Api(value = "帮助中心",tags = {"帮助中心"})
@RequestMapping("")
public class HelpCenterController extends TikTokBaseController {
    @Value("${server.domain}")
    String serverDomain;

    @Autowired
    HelperService helperService;
    List<HelpCenterMenuDto> menus;
    @PostConstruct
    public void init() {
         menus = helperService.findAllHelpCenterMenuDtos();

    }

    /**
     * 帮助中心首页
     *
     * @return
     */
    @GetMapping("/help/home")
    public ModelAndView articleHome(HttpServletRequest request){
        return article("","",1l,request);
    }
    /**
     * 查看文章
     * 日后有文章查看数需求，可在这里的下游完成update表后再返回表记录
     * @param articleId 文章id，
     * @param suffix 文章路径后缀，用于日志记录
     * @param articleId 文章id
     * @return
     */
    @GetMapping("/help-art/{articleId}/{suffix}")
    public ModelAndView article(@PathVariable("suffix") String suffix, String viewMarkInfo,@PathVariable("articleId")  Long articleId,HttpServletRequest request) {
        log.info("param.suffix={},param.articleId={},param.viewMarkDto={}",suffix,articleId,viewMarkInfo);
        long start=System.currentTimeMillis();

        String viewName="helpCenter/help";
        ModelAndView modelAndView = new ModelAndView(viewName);

        //文章
        HelpArticle helpArticle=helperService.findHelpCenterArticleDtoById(articleId);
        if(helpArticle==null){
            helpArticle=new HelpArticle();
        }
        if(viewMarkInfo==null){
            viewMarkInfo="";
        }

        modelAndView.addObject("login", getUser() != null);
        modelAndView.addObject("isMobile", CheckMobile.check());
        //菜单列表
        modelAndView.addObject("tab",2);
        modelAndView.addObject("menuList",menus);
        modelAndView.addObject("viewMarkInfo",viewMarkInfo);
        modelAndView.addObject("currentMenuId",helpArticle.getMenuId());
        modelAndView.addObject("contextPath",serverDomain);
        modelAndView.addObject("article",helpArticle);



        System.out.println("menuList="+JsonUtil.parseJson(menus));
        System.out.println("mennuid="+helpArticle.getMenuId()+":articleid"+helpArticle.getId());
        log.info("Scheme={},contextPath={},param.suffix={},param.articleId={},param.viewMarkDto={},times={},currentMenuId={}",request.getScheme(),serverDomain,suffix,articleId,viewMarkInfo,System.currentTimeMillis()-start,helpArticle.getMenuId());
        return modelAndView;
    }
    /**
     * 查看文章列表 分页
     * @param id 菜单id
     * @return
     */
    @GetMapping("/help-arts/{id}")
    public ModelAndView menuArticlesPage(@PathVariable("id") Long id, String viewMarkInfo,Integer pageNumber,Integer pageSize,HttpServletRequest request) {
//        if(pageNumber==null||pageNumber<=0){
//            pageNumber=1;
//        }
//        if(pageSize==null||pageSize<=0){
//            pageSize=10;
//        }
        //分页强制取消，但model格式不变
        pageNumber=1;
        pageSize=1000;
        if(viewMarkInfo==null){
            viewMarkInfo="";
        }
        log.info("param.id={},param.viewMarkDto={},pageNumber={},pageSize={}",id,viewMarkInfo,pageNumber,pageSize);
        long start=System.currentTimeMillis();
        String viewName="helpCenter/help";
        ModelAndView modelAndView = new ModelAndView(viewName);

        Page<HelpCenterArticleSummaryDto> articleList=helperService.findAllArticlesSummary(id, pageNumber, pageSize);
        modelAndView.addObject("isMobile", CheckMobile.check());
        modelAndView.addObject("tab",2);
        modelAndView.addObject("login", getUser() != null);
        modelAndView.addObject("menuList",menus);
        modelAndView.addObject("viewMarkInfo",viewMarkInfo);
        modelAndView.addObject("currentMenuId",id);
        modelAndView.addObject("contextPath",serverDomain);
        modelAndView.addObject("articles",articleList);
        System.out.println("menuList="+JsonUtil.parseJson(menus));
        System.out.println("articles ="+JsonUtil.parseJson(articleList));
        log.info("param.id={},param.viewMarkDto={},times={},currentMenuId={}",id,viewMarkInfo,System.currentTimeMillis()-start,id);
        return modelAndView;
    }
    /**
     * 查看文章列表
     * @param id 菜单id
     * @return
     */
//    @GetMapping("/articles/{id}")
//    public ModelAndView menuArticles(@PathVariable("id") Long id, String viewMarkInfo,HttpServletRequest request) {
//
//
//        log.info("param.id={},param.viewMarkDto={}",id,viewMarkInfo);
//        long start=System.currentTimeMillis();
//        String viewName="helpCenter/help";
//        ModelAndView modelAndView = new ModelAndView(viewName);
//
//        List<HelpCenterArticleSummaryDto>  articleList=helperService.findAllArticlesSummary(id);
//        modelAndView.addObject("menuList",menus);
//        modelAndView.addObject("viewMarkInfo",viewMarkInfo);
//        modelAndView.addObject("contextPath",getContextPath(request));
//        modelAndView.addObject("articleList",articleList);
//        System.out.println("menuList="+JsonUtil.parseJson(menus));
//        System.out.println("articleList="+JsonUtil.parseJson(articleList));
//        log.info("param.id={},param.viewMarkDto={},times={}",id,viewMarkInfo,System.currentTimeMillis()-start);
//        return modelAndView;
//    }

    /**
     * 更新菜单
     * @return
     */
    @GetMapping("/menu/refresh")
    @ResponseBody
    public  List<HelpCenterMenuDto> menuRefresh() {
        menus=helperService.findAllHelpCenterMenuDtos();
        return menus;
    }

//     private String getContextPath(HttpServletRequest request){
//        return  request.getScheme() +"://" + request.getServerName()  + ":" +request.getServerPort() +request.getContextPath();
//     }

//    private List<HelpCenterMenuDto>  setUrlPath(List<HelpCenterMenuDto> menuDtoList,HttpServletRequest request) throws IOException, ClassNotFoundException {
//        String contextPath = request.getScheme() +"://" + request.getServerName()  + ":" +request.getServerPort() +request.getContextPath();
//        //list深度拷贝
//        List<HelpCenterMenuDto> newList = deepCopy(menuDtoList);
//
//        newList.forEach(menu->{
//            if(menu.getUrl()!=null){
//                menu.setUrl(contextPath+"/"+menu.getUrl());
//            }
//
//        });
//        return newList;
//    }
//    private static <T> List<T> deepCopy(List<T> src) throws IOException, ClassNotFoundException {
//        ByteArrayOutputStream out=new ByteArrayOutputStream();
//        ObjectOutputStream objectOutputStream=new ObjectOutputStream(out);
//        objectOutputStream.writeObject(src);
//
//        ByteArrayInputStream byeIn=new ByteArrayInputStream(out.toByteArray());
//        ObjectInputStream in=new ObjectInputStream(byeIn);
//        @SuppressWarnings("unchecked")
//        List<T> rst=(List<T>) in.readObject();
//        return rst;
//
//    }
}
